<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Game extends Controller_Admin_Based
{

    public function action_list()
    {

        $pagination_arr = Paginationhelp::get_data('game', 'id_game');

        $types = Type::instances()->get_type();

        $data = array(
            'result' => $pagination_arr['data'],
            'types' => $types,
            'page_view' => $pagination_arr['pagination']
        );

        $view = Viewer::display('admin/game_list', $data);
        $this->response->body($view);
    }
    
 

    public function action_add()
    {
        $types = Type::instances()->get_type();
        $data = array(
            'types' => $types,
        );
        $view = Viewer::display('admin/game_add', $data);
        $this->response->body($view);
    }

    public function action_do_add()
    {

        if ($_POST)
        {
            if ($_POST['addtime'] == '')
            {
                $_POST['addtime'] = time();
            }
            else
            {
                $_POST['addtime'] = Time::instance()->get_mk_time($_POST['addtime']);
            }
            $_POST['updatetime'] = time();

            $logo = Validation::factory($_FILES);
            if ($logo->check())
            {
                $logo_path = Uploads::handleupload('/uploads/logo', $_FILES['logo']);
            }
            $_POST['logo'] = $logo_path;
            $images = Validation::factory($_FILES);
            if ($images->check())
            {
                $images_path = Uploads::handleupload('/uploads/images', $_FILES['images']);
            }
            $_POST['images'] = $images_path;
            $game = Game::instances()->set($_POST);
            if ($game)
            {
                Message::set('游戏添加成功！');
                Request::current()->redirect('/admin/game/add');
            }
        }
    }

    public function action_edit()
    {
        $id_game = $this->request->param('id');

        $game = Game::instances($id_game)->get();
        $types = Type::instances()->get_type();
        $view = Viewer::display('admin/game_edit', array('data' => $game, 'types' => $types));
        $this->response->body($view);
    }

    public function action_do_edit()
    {
        $id_game = $this->request->param('id');
        $game = Game::instances($id_game)->get();
        if ($_POST)
        {
            $_POST['updatetime'] = time();

            if ($_FILES['logo']['tmp_name'] != '')
            {
                $logo = Validation::factory($_FILES);
                if ($logo->check())
                {
                    $logo_path = Uploads::handleupload('/uploads/logo', $_FILES['logo']);
                }
                $_POST['logo'] = $logo_path;
            }
            else
            {
                $_POST['logo'] = $game['logo'];
            }


            if ($_FILES['images']['tmp_name'] != '')
            {
                $images = Validation::factory($_FILES);
                if ($images->check())
                {
                    $images_path = Uploads::handleupload('/uploads/images', $_FILES['images']);
                }
                $_POST['images'] = $images_path;
            }
            else
            {
                $_POST['images'] = $game['images'];
            }
            $game = Game::instances($id_game)->update($_POST);
            if ($game)
            {
                Message::set('游戏编辑成功！');
                Request::current()->redirect('/admin/game/edit/' . $id_game);
            }
        }
        $game = Game::instances($id_game)->get();
        $types = Type::instances()->get_type();
        $view = Viewer::display('admin/game_edit', array('data' => $game, 'types' => $types));
        $this->response->body($view);
    }

    public function action_delete()
    {
        $id_game = $this->request->param('id');
        $flag = Game::instances($id_game)->delete();
        if ($flag)
        {
            Message::set('游戏删除成功');
            Request::current()->redirect('/admin/game/list');
        }
        else
        {
            Message::set('游戏删除失败', 'error');
            Request::current()->redirect('/admin/game/list');
        }
    }

}